package timetable.io;

import timetable.exceptions.InvalidEntryException;

/**
 * Contains all content regarding a time table entry.
 * 
 * @author Sander Eskens
 *
 */
public class TimeTableEntry {

	public final static String TIME_REGEX = "^[0-9]?[0-9][u:h][0-9][0-9]$";

	private String duur;
	private String lokaal;
	private String klas;
	private String aanvang;
	private String docent;
	private String cursusnaam;
	private String dag;
	private int id;
	
	// FIELDS WHEN ENTRY IS INVALID
	private InvalidEntryException error;
	
	public TimeTableEntry()
	{
		
	}
	
	public TimeTableEntry(int id)
	{
		this.id = id;
	}

	public void setDuur(String duur) {
		this.duur = duur;
	}


	public String getDuur() {
		return duur;
	}


	public void setLokaal(String lokaal) {
		this.lokaal = lokaal;
	}


	public String getLokaal() {
		return lokaal;
	}


	public void setKlas(String klas) {
		this.klas = klas;
	}


	public String getKlas() {
		return klas;
	}


	public void setAanvang(String aanvang) {
		this.aanvang = aanvang;
	}


	public String getAanvang() {
		return aanvang;
	}


	public void setDocent(String docent) {
		this.docent = docent;
	}


	public String getDocent() {
		return docent;
	}


	public void setCursusnaam(String cursusnaam) {
		this.cursusnaam = cursusnaam;
	}


	public String getCursusnaam() {
		return cursusnaam;
	}


	public void setDag(String dag) {
		this.dag = dag;
	}


	public String getDag() {
		return dag;
	}
	
	@Override
	public String toString()
	{
		return aanvang + "\t" + duur + "\t" + cursusnaam + "\t" + docent + "\t" + klas + "\t" + lokaal; 
	}
	
	public void check() throws InvalidEntryException
	{
		InvalidEntryException e;
		if (aanvang == null) {
			e = new InvalidEntryException(id, "ParserError", DataField.START + " not found");
			this.setInvalid(e);
			throw e;
		}
		else
		{
			if (!aanvang.matches(TIME_REGEX)) 
			{
				e = new InvalidEntryException(id, "ParserError", DataField.START + " has an invalid time-syntax");
				this.setInvalid(e);
				throw e;
			}
		}
		if (duur == null) {
			e = new InvalidEntryException(id, "ParserError", DataField.DURATION + " not found");
			this.setInvalid(e);
			throw e;
		}
		else
		{
			if (!duur.matches(TIME_REGEX))
			{
				e = new InvalidEntryException(id, "ParserError", DataField.DURATION + " has an invalid time-syntax");
				this.setInvalid(e);
				throw e;
			}
		}
	}
	
	private void setInvalid(InvalidEntryException e)
	{
		this.error = e;
	}
	
	public void setInvalid(String message)
	{
		this.setInvalid(new InvalidEntryException(this.id, "PARSER ERROR", message));
	}
	
	public boolean isInvalid()
	{
		return (this.error != null);
	}

	public int getId() {
		return id;
	}

	public InvalidEntryException getError() {
		return error;
	}
}
